package com.hh.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hh.annotation.AuthCheck;
import com.hh.common.BaseResponse;
import com.hh.common.DeleteRequest;
import com.hh.common.ErrorCode;
import com.hh.common.ResultUtils;
import com.hh.constant.UserConstant;
import com.hh.entity.domain.Notice;
import com.hh.entity.domain.User;
import com.hh.entity.dto.notice.NoticeAddRequest;
import com.hh.entity.dto.notice.NoticeQueryRequest;
import com.hh.entity.dto.notice.NoticeUpdateRequest;
import com.hh.entity.vo.NoticeVO;
import com.hh.exception.BusinessException;
import com.hh.service.MessageService;
import com.hh.service.NoticeService;
import com.hh.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 公告控制器类，提供公告的增删改查等接口
 * @author hanhui
 * @version 0.0.1
 * @date 2025/3/5
 **/
@RestController
@Slf4j
@RequestMapping("/notice")

public class NoticeController {

    @Resource
    private NoticeService noticeService;


    @Resource
    private UserService userService;
    @Resource
    private MessageService messageService;

    /**
     * 添加公告
     *
     * @param noticeAddRequest 公告对象
     * @return 添加结果
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addNotice(@RequestBody NoticeAddRequest noticeAddRequest,HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Notice notice = noticeService.addNotice(noticeAddRequest, loginUser);
        messageService.sendSystemMessage(notice.getContent());
        return ResultUtils.success(notice.getId());
    }

    /**
     * 删除公告
     *
     * @param deleteRequest 公告ID
     * @return 删除结果
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteNotice(DeleteRequest deleteRequest,HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        noticeService.deleteNotice(deleteRequest, loginUser);
        return ResultUtils.success(true);
    }

    /**
     * 更新公告
     *
     * @param noticeUpdateRequest 公告修改请求
     * @return 更新结果
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateNotice(
            @RequestBody NoticeUpdateRequest noticeUpdateRequest,
            HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        noticeService.updateNotice(noticeUpdateRequest, loginUser);
        return ResultUtils.success(true);
    }

    /**
     * 根据ID查询公告 （仅管理员可用）
     *
     * @param id 公告ID
     * @return 公告对象
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Notice> getNoticeById(Long id) {
        log.info("Getting notice with ID: {}", id);
        Notice notice = noticeService.getById(id);
        if (notice != null) {
            return ResultUtils.success(notice);
        } else {
            throw new BusinessException(ErrorCode.NOTICE_NOT_FOUND, "公告未找到");
        }
    }
    /**
     * 根据ID查询公告
     *
     * @param id 公告ID
     * @return 公告对象
     */
    @GetMapping("/get/vo")
    public BaseResponse<NoticeVO> getNoticeById(Long id,HttpServletRequest request) {
        if (id == null||id<=0){
            throw new BusinessException(ErrorCode.PARAMS_NULL);
        }
        Notice notice = noticeService.getById(id);
        if (notice == null){
            throw new BusinessException(ErrorCode.NOTICE_NOT_FOUND, "公告未找到");
        }
        return ResultUtils.success(noticeService.getNoticeVo(notice,request));
    }


    /**
     * 分页查询公告
     *
     * @param noticeQueryRequest 公告查询请求对象
     * @return 公告列表
     */
    @GetMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<Notice>> listNoticeByPage( NoticeQueryRequest noticeQueryRequest) {

        int page = noticeQueryRequest.getPageNum();
        int pageSize = noticeQueryRequest.getPageSize();
        Page<Notice> noticePage = noticeService.page(new Page<>(page, pageSize),
                noticeService.getQueryWrapper(noticeQueryRequest));
        return ResultUtils.success(noticePage);
    }

    /**
     * 分页查询公告
     *
     * @param noticeQueryRequest 公告查询请求对象
     * @return 公告列表
     */
    @GetMapping("/list/page/vo")
    public BaseResponse<Page<NoticeVO>> listNoticeVObyPage(
              NoticeQueryRequest noticeQueryRequest) {
        if (noticeQueryRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_NULL);
        }
        int page = noticeQueryRequest.getPageNum();
        int pageSize = noticeQueryRequest.getPageSize();
        Page<Notice> noticePage = noticeService.page(new Page<>(page, pageSize),noticeService.getQueryWrapper(noticeQueryRequest));
        Page<NoticeVO> noticeVoPage = noticeService.getNoticeVoPage(noticePage);
        return ResultUtils.success(noticeVoPage);
    }
}
